app.controller("z-salaryApproval-c", ['$scope', '$rootScope', '$z-http', '$sce', 'z-salaryApproval-s', 'z-salaryDetail-s', 'z-salaryRequest-s', 'z-dictionary-s', 'z-corporation-s', 'z-department-s', 'z-edit-menu-s', 'z-attendance-s', '$filter',
    function ($scope, $rootScope, $zhttp, $sce, Service, SalaryDetailService, RequestService, DictionaryService, CorporationService, DepartmentService, MenuService, AttendanceService, $filter) {
        $scope.viewModel = {};
        $scope.viewModel.perPage = "10";
        $scope.viewModel.rowCount = "1";
		$scope.viewModel.approvalState=""         //审批状态
		$scope.viewModel.checkAll=false;          //全选状态
		
		//保存复选框状态
		$scope.viewModel.checkboxDataList=[];
		$scope.viewModel.approvalState=1;
        
		//下拉多选框长度
		$scope.multiSelectMaxHeight=300;
		$scope.editModel= {};
		$scope.editModel.title="";
		$scope.editModel.approvalStateList=[];
		$scope.viewModel.requestTypeList = ["0","1","2","3","4","5","6","10","11","12","13"];
		$scope.viewModel.employeeNo=null;
		$scope.viewModel.employeeList=[];
		$scope.viewModel.approvalTypeList = [];
		
        //日期筛选条件
        $scope.viewModel.reportStartDate = null;
        $scope.viewModel.reportEndDate = null;
        
        //查询页面名称
        MenuService.getMenuByUrl({url: 'salaryApproval'},
            function (data) {
                $scope.viewModel.menuList = data;
            }, function (error) {
            });
        $scope.viewModel.approvalStateList = [];
        // 获取本月第一天
        $scope.viewModel.reportStratDate = (new Date()).setDate(1);
        // 获取当前日期
        $scope.viewModel.reportEndDate = new Date();

        //日期控件
        $scope.viewModel.dateOptions = {
            formatYear: 'yy',
            startingDay: 1,
            showWeeks: false
        };
        //日期控件
        $scope.viewModel.format = "yyyy/MM/dd";
        //日期启动开关
        $scope.viewModel.startDateOpened = false;
        $scope.viewModel.endDateOpened = false;
        //起止日期选择
        $scope.viewModel.startDateOpen = function ($event) {
            $event.preventDefault();
            $event.stopPropagation();
            $scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
        };
        //起止日期选择
        $scope.viewModel.endDateOpen = function ($event) {
            $event.preventDefault();
            $event.stopPropagation();
            $scope.viewModel.endDateOpened = !$scope.viewModel.endDateOpened;
        };
        //加载审批状态数据
        $scope.viewModel.bindSelectData = function () {
            AttendanceService.getDictionaryListBydicCls({
                    dicCls: "10"
                },
                function (data) {
                    $scope.viewModel.approvalStateList = data;
                  //遍历并判断是否为通过或拒绝
                    for(var i=0;i<$scope.viewModel.approvalStateList.length;i++){
                        if($scope.viewModel.approvalStateList[i].dicCd=='2' || $scope.viewModel.approvalStateList[i].dicCd=='3'){
                            $scope.editModel.approvalStateList.push($scope.viewModel.approvalStateList[i]);
                        }
                    }
                }, function (error) {
                	
                });
        };
        $scope.viewModel.bindSelectData();

        $scope.viewModel.employeeIsSeparated = null;
        $scope.viewModel.regionType = null;
        $scope.viewModel.employeeNo = null;
        $scope.viewModel.roleName = null;
        $scope.viewModel.departmentList = [];
        $scope.viewModel.corporationList = [];
        $scope.viewModel.employeeList = [];
        $scope.viewModel.level = null;

        //判断是否为全选
        $scope.viewModel.JudgeCheckboxAll = function () {
            var type = false;
            for (var i = 0; i < $scope.viewModel.dataList.length; i++) {
                if ($scope.viewModel.dataList[i].checkbox == false) {
                    type = false;
                    break;
                } else {
                    type = true;
                }
            }
            if (type) {
                $scope.viewModel.checkAll = true;
            } else {
                $scope.viewModel.checkAll = false;
            }
        };
        //全选事件
        $scope.viewModel.checkboxAll = function () {
            //判断是否为全选
            console.log("is check all", $scope.viewModel.checkAll);
            if ($scope.viewModel.checkAll == false) {
                angular.forEach($scope.viewModel.dataList, function (value, key) {
                	if (value.approvalState==1) {
                		value.checkbox = true;
					}
                });
            } else {
                angular.forEach($scope.viewModel.dataList, function (value, key) {
                    value.checkbox = false;
                });
            }
        };

        //单选事件
        $scope.viewModel.checkbox = function (row) {
            if (row.checkbox) {
                row.checkbox = false;
            } else {
                row.checkbox = true;
            }
            $scope.viewModel.JudgeCheckboxAll();
        };


        //加载数据
        $scope.viewModel.bindData = function () {
            $scope.viewModel.employeeList = [];
            if ($scope.viewModel.employeeNo != null) {
                var employeeNoData = $scope.viewModel.employeeNo.split("\n");
                for (var i = 0; i < employeeNoData.length; i++) {
                    $scope.viewModel.employeeList.push(employeeNoData[i]);
                }
            }
            $scope.viewModel.isLoading = true;
            //获取基本信息
            Service.getSalaryRequestListByApl({
                    pageNo: $scope.viewModel.rowCount,
                    pageCount: $scope.viewModel.perPage,
                    approvalState: $scope.viewModel.approvalState,
                    selStartDate: $scope.viewModel.reportStratDate,
                    selEndDate: $scope.viewModel.reportEndDate,
                    employeeList:$scope.viewModel.employeeList,
                },
                function (data) {
                    var list = data.salaryRequestBeanList.map(function (value) {
                        value.checkbox = false;
                        return value;
                    });
                    $scope.viewModel.checkAll = false;
                    $scope.viewModel.dataList = list;
                    $scope.viewModel.totalData = data.pageTotal;
                    $scope.viewModel.isLoading = false;
                }, function (error) {
                    $scope.viewModel.isLoading = false;
                });
        };
        $scope.viewModel.bindData();

        $scope.viewModel.ulShow = false;


        $scope.approvalRecord = {};
        $scope.viewModel.selectApprovalRecord = function (row) {
            if (row.approvalState != 0) {
                $scope.approvalRecord.show = true;
                $scope.viewModel.isLoading = true;
                $scope.approvalRecord.title = "查看审批过程";
                $scope.approvalRecord.dataList = null;
                //获取基本信息
                RequestService.getSalayApprovalRecordList({
                        salaryRequestId: row.salaryRequestId,
                        salaryRuleId: row.salaryRuleId
                    },
                    function (data) {
                        $scope.approvalRecord.dataList = data;
                        $scope.viewModel.isLoading = false;
                    }, function (error) {
                        $rootScope.alertError(error.fault.message);
                        $scope.viewModel.isLoading = false;
                    });
            }
        };
        //关闭加载
        $scope.viewModel.unLoad=function(){
            $scope.approvalRecord.show=false;
            $scope.detailModel.show = false;
            $scope.editModel.show = false;
        };

        // 显示明细
        $scope.detailModel = {
            title: "明细"
        };
        //下载报表
        $scope.viewModel.download = function (row) {
            $scope.viewModel.isLoading = true;
            RequestService.getDownloadSalary({
            	salaryRequestId: row.salaryRequestId
            }, function (data) {
                $scope.viewModel.isLoading = false;
                window.location.href = data;
            }, function (error) {
                $scope.viewModel.isLoading = false;
                $rootScope.alertError(error.fault.message);
            });
        };
        
        $scope.viewModel.showDetail = function (row) {
        	window.open("#/salaryDetail?salaryRequestId="+row.salaryRequestId);
        };

        $scope.viewModel.approval = function () {
            var checked = $scope.viewModel.dataList.filter(function (val) {
                return val.checkbox == true;
            })
            if(checked.length<=0){
                $rootScope.alertWarning("未选择申请审批信息！");
                return;
            }
            $scope.editModel = {};
            $scope.editModel.title="申请审批";
            $scope.editModel.raApproval={};
            $scope.editModel.requestApproval={};
            $scope.editModel.requestApproval.approvalState=2;
            $scope.editModel.approvalStateList = [];
            //遍历并判断是否为通过或拒绝
            for(var i=0;i<$scope.viewModel.approvalStateList.length;i++){
                if($scope.viewModel.approvalStateList[i].dicCd=='2' || $scope.viewModel.approvalStateList[i].dicCd=='3'){
                    $scope.editModel.approvalStateList.push($scope.viewModel.approvalStateList[i]);
                }
            }
            //验证不能为空
            $scope.editForm.approvalState.$dirty=false;
            $scope.editModel.show = true;
        };

        //验证
        $scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
                if(!$scope.editForm.approvalState.$dirty){
                    $scope.editForm.approvalState.$dirty=true;
                }
                return true;
            }else{
                return false;
            }
        };

        // 审批
        $scope.viewModel.save = function(){
            if($scope.viewModel.requiredForm()){
                return;
            }
            var salaryRequestIdList = [];
            $scope.viewModel.dataList.map(function (val) {
                if ( val.checkbox == true ) {
                    salaryRequestIdList.push(val.salaryRequestId);
                }
            });
            var params = {
                salaryRequestIdList: salaryRequestIdList,
                approvalState: $scope.editModel.requestApproval.approvalState,
                description: $scope.editModel.requestApproval.description
            };
            $scope.viewModel.isLoading = true;
            Service.saveSalaryApprovalRequestList(params,
                function(data){
                    $scope.viewModel.unLoad();
                    $scope.viewModel.bindData();
                    $rootScope.alertInfo("申请审批完成。");
                    $scope.viewModel.isLoading = false;
                },function(error) {
                    $rootScope.alertError(error.fault.message);
                    $scope.viewModel.isLoading = false;
                });
        };
        
        //判断是不是最终审批人，1是0不是
        $scope.viewModel.isLastPeople = function(){
        	Service.getIsLastPeople({},
                function(data){
    				$scope.viewModel.isLastPeo = data;
                },function(error) {
                });
        };
        //驳回工资审批
        $scope.viewModel.rejectSalary = function(e){
        	$rootScope.alertConfirm("确定驳回审批？",function() {
                $scope.viewModel.isLoading=true;
                Service.saveRejectSalary({
                	salaryRequestId: e.salaryRequestId
        		},
                function (data) {
                    $scope.viewModel.bindData();
                    $scope.viewModel.isLoading=false;
                    $rootScope.alertInfo("驳回成功");
                },
                function (error) {
                    $scope.viewModel.isLoading=false;
                    $rootScope.alertWarning(error.fault.message);
                });
            });
        };
        
        $scope.viewModel.isLastPeople();
        
        // 设置初始值
        $scope.viewModel.checkbox1 = true;
        $scope.viewModel.checkbox2 = true;
        $scope.viewModel.checkbox3 = true;
        $scope.viewModel.checkbox4 = true;
        $scope.viewModel.checkbox5 = true;
        $scope.viewModel.checkbox6 = true;
        $scope.viewModel.checkbox7 = true;
        $scope.viewModel.checkbox8 = true;
        $scope.viewModel.checkAll = false;

    }]);